package com.qf.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.qf.base.dto.face.FaceDTO;
import com.qf.base.dto.sms.SmsDTO;
import com.qf.base.result.R;
import com.qf.base.utils.JWTUtil;
import com.qf.base.utils.PropertiesUtil;
import com.qf.base.vo.UserVo;
import com.qf.user.client.FaceClient;
import com.qf.user.client.SmsClient;
import com.qf.user.dto.OneKeySingInDTO;
import com.qf.user.dto.SignInDto;
import com.qf.user.model.User;
import com.qf.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Random;

/**
 * projectName: automation
 *
 * @author dongshihao
 * Date:2021/9/24   21:40
 * description:
 */
@RestController
@RequestMapping("qf")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private SmsClient smsClient;
    @Resource
    private FaceClient faceClient;


    /**
     * 测试
     *
     * @return
     */
    @GetMapping("test")
    public R test() {
        return R.ok();
    }

    @PostMapping("signIn")
    public R signIn(@RequestBody SignInDto signInDto) {
        String signInName = signInDto.getSignInName();
        String password = signInDto.getPassword();

        User user = userService.signIn(signInName, password);
        if (user == null) {
            //判读是否锁上了
            return R.error(501, "登录信息输入有无");
        } else {
            if (user.getIsLocked() == 1) {
                //锁定
                return R.error(502, "当前账户已被锁定");
            } else {
                //登录成功
                // 返回前端的数据
                UserVo userVo = new UserVo();
                //User --> userVO
                PropertiesUtil.copy(user, userVo);
                userVo.setPassword("");
                //签发 token
                String token = JWTUtil.newToken(userVo);
                System.out.println("userVo = " + userVo.toString());
                return R.ok("登录成功")
                        .setResult("token", token)
                        .setResult("user", userVo);

            }
        }

    }

    /**
     * 请求验证吗
     */
    @PostMapping("sms")
    public R sms(@RequestBody SmsDTO smsDTO) {
        //说明自己的模块id
        smsDTO.setServiceId(1);
        //生成六位数验证码
        Random random = new Random();
        String code = random.nextInt(1000000) + "";
        int iMax = 6 - code.length();

        for (int i = 0; i < iMax; i++) {
            code = 0 + code;
        }
        smsDTO.setCode(code);
        smsDTO.setMsg("您的验证码为" + code + "，正在进行登录操作，请勿泄漏此短信内容");
        System.out.println("iMax = " + iMax);
        System.out.println("code = " + code);
        //调用短信服务，发送短信
        return smsClient.send(smsDTO);
    }


    /**
     * 手机号一件登录
     */
    @PostMapping("oneKeySingIn")
    public R oneKeySingIn(@RequestBody OneKeySingInDTO oneKeySingInDTO) {
        String phone = oneKeySingInDTO.getPhone();
        String code = oneKeySingInDTO.getCode();

        SmsDTO smsDTO = new SmsDTO();
        smsDTO.setPhoneNo(phone);
        smsDTO.setCode(code);
        smsDTO.setServiceId(1);
        R checkResult = smsClient.check(smsDTO);
        if (checkResult.isOK()) {
            //短信验证正确
            User user = userService.oneKeySignIn(phone);
            UserVo userVo = new UserVo();
            PropertiesUtil.copy(user, userVo);
            String token = JWTUtil.newToken(userVo);

            return R.ok("登录成功！")
                    .setResult("token", token)
                    .setResult("user", userVo);

        } else {
            return checkResult;
        }
    }

    /**
     * : status 500 reading SmsClient#check(SmsDTO)
     *
     *
     */

    /**
     * OA 签到的接口
     */
    @PostMapping("faceSign")
    public R faceSign(@RequestBody FaceDTO faceDTO) {

        //请求人脸识别子服务

        faceDTO.setServiceId(3);
        R faceResult = faceClient.search(faceDTO);


        if (faceResult.isOK()) {
            JSONObject result = new JSONObject((Map<String, Object>) faceResult.getResult());
            Long userId = result.getLong("userId");

            //查询数据库
            User user = userService.findUserById(userId);
            UserVo userVo = new UserVo();

            PropertiesUtil.copy(user, userVo);
            userVo.setPassword("");
            String token = JWTUtil.newToken(userVo);
            return R.ok().setResult("user",userVo).setResult("token",token);


        }
        return R.error("识别失败！");


    }

    /**
     * 人脸登录的接口
     * saas 软件及服务
     * paas 平台
     * iaas 基础设施
     * baas
     * faas
     */
    @PostMapping("sign")
    public R sign(@RequestBody FaceDTO faceDTO) {
        //请求人脸识别子服务
        faceDTO.setServiceId(1);
//        R faceResult = faceClient.send(faceDTO);
        R faceResult = faceClient.search(faceDTO);
        if (faceResult.isOK()) {
            JSONObject result = new JSONObject((Map<String, Object>) faceResult.getResult());

            Long userId = Long.valueOf(result.getInteger("userid"));
//            Long userId = result.getLong("userId");

            //查询数据库
            User user = userService.findUserById(userId);
            UserVo userVo = new UserVo();
            PropertiesUtil.copy(user, userVo);
            return R.ok().setResult("user",userVo);


        }
        return R.error("识别失败！");
    }
}
